<template>
  <!-- 文件列表 -->
  <div class="filelist-main">
    <div class="query_list">
      <el-form
        ref="form"
        :inline="true"
        :model="formData"
        label-width="85px"
        size="mini"
        style="min-width: 940px"
      >
        <el-form-item label="输入搜索：">
          <el-input
            placeholder="文件名/上传人"
            v-model="formData.keyword"
            style="width: 140px"
          ></el-input>
        </el-form-item>
        <el-form-item label="数据类型：">
          <el-select v-model="formData.dataType" placeholder="请选择数据类型">
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="(item, index) in addressObjects"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据来源：">
          <el-select v-model="formData.dataOrigin" placeholder="请选择数据来源">
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="(item, index) in dataOrigins"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="getList"
            >搜索</el-button
          >
          <el-button size="mini" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          prop="id"
          label="文件编号"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="file_name"
          label="文件名"
          width="130"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="dataTypeName" label="数据类型"></el-table-column>
        <el-table-column
          prop="dataSourceName"
          label="数据来源"
        ></el-table-column>
        <el-table-column prop="user_name" label="最新上传人"></el-table-column>
        <el-table-column
          prop="update_time"
          label="最新上传时间"
          width="140"
          :formatter="(a) => formatterBannerOs(a.update_time)"
        ></el-table-column>
        <el-table-column prop="import_status" label="导入结果">
          <template slot-scope="scope">
            <p :class="importResMaps[scope.row.import_status]">
              {{ scope.row.import_status }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="success_count" label="成功/失败">
          <template slot-scope="scope">
            <div class="color1">
              <span class="color1">{{ scope.row.success_count }}</span
              >/
              <span class="color2">{{ scope.row.fail_count }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="fail_message" label="失败原因" width="150">
          <template slot-scope="scope">
            <div v-if="scope.row.import_status === '部分成功' || scope.row.import_status === '导入失败'">
              请到
              <el-button
                type="text"
                size="small"
                @click="failFileClick(scope.row.id, scope.row.file_name)"
                >失败文件</el-button
              >查看
            </div>
            <div v-else>{{ scope.row.fail_message || "暂无填写原因" }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="handle_status"
          label="处理状态"
        ></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              type="text"
              size="small"
              v-if="scope.row.handle_status === '待处理' && showExportMaps[scope.row.import_status]"
            >继续导入</el-button>
            <el-button type="text" size="small" @click="addrHandle(scope.row.id)" v-if="(scope.row.handle_status === '待处理') && (scope.row.import_status === '部分成功' || scope.row.import_status === '导入成功')">启动地址处理</el-button>
            <el-button type="text" size="small" @click="deleteOneAddress(scope.row.id)">删除</el-button>
            <!-- <el-button type="text" size="small" @click="failFileClick(scope.row.id)">下载失败文件</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 继续导入 -->
      <fileImportWin
        :visible="continueImportingDialogShow"
        :onOk="(e) => {closeDialog('continueImportingDialogShow', e);getList()}"
        :confirmSubmit="(e) => confirmSubmit(e)"
        :titleData="titleData"
      ></fileImportWin>

      <el-pagination
        class="pagination"
        small
        layout="prev, pager, next"
        :total="total_num"
        @current-change="pageChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import VueRouter from "@/env/router";
import fileImportWin from "@/components/fileList/fileImportWin";

import {
  deleteEmptyParam,
  formatterBannerOs,
  downLoadTempFunc,
} from "@/utils/index.js";

export default {
  components: {
    fileImportWin,
  },
  mounted() {
    this.getList();
    this.getSelectList();
  },
  data() {
    return {
      formData: {
        keyword: "",
        dataType: "",
        dataOrigin: "",
        status: "",
      },
      showExportMaps: {
        部分成功: true,
        导入失败: true,
        导入成功: true,
      },
      formatterBannerOs,
      titleData: {
        id: "",
        name: "",
      },
      continueImportingDialogShow: false,
      importResMaps: {
        导入成功: "color1",
        导入失败: "color2",
        部分成功: "color3",
        等待导入: "color4",
        导入中: "color5",
      },
      tableData: [],
      pager: {
        page: 1,
        limit: 10,
      },
      dataOrigins: [],
      addressObjects: [],
      total_num: 0,
    };
  },
  methods: {
    closeDialog(dialogStr) {
      this[dialogStr] = false;
    },
    handleClick(row) {
      console.log(row, "row");

      this.continueImportingDialogShow = true;
      this.$nextTick(() => {
        this.titleData = {
          id: row.id,
          file_name: row.file_name,
        };
      });
    },
    confirmSubmit(e) {
      console.log(e, "confirmSubmit");
    },
    async addrHandle(id) {
      const res = await this.axios.post(this.apis.addressDataUpdate, {
        id,
        handle_status: "处理中",
      });
      console.log(res, "res");
      if (res.retCode === 2000) {
        VueRouter.push("/addrProcess?id=" + id);
      } else {
        this.$message.warning(res.retMessage);
      }
    },
    deleteOneAddress(id) {
      this.$confirm("确认要删除吗？", "提示").then(async () => {
        try {
          const res = await this.axios.post(this.apis.importFileRecordDelete, {
            id,
          });
          this.$message.success("删除成功");
          this.getList();
        } catch (err) {
          this.$message.warning(err);
        }
      });
    },
    async getList() {
      let formData = JSON.parse(JSON.stringify(this.formData));
      const res = await this.axios.post(
        this.apis.importFileRecordQuery,
        deleteEmptyParam({
          keyword: formData.keyword,
          // handle_status: '已完成',
          data_type: formData.dataType,
          data_source: formData.dataOrigin,
          page: this.pager.page,
          limit: this.pager.limit,
        })
      );
      console.log(res, "res");
      if (res.retCode === 2000) {
        this.tableData = res.rows.items;
        this.total_num = res.rows.total_num;
      }
    },
    onReset() {
      (this.formData = {
        keyword: "",
        dataType: "",
        dataOrigin: "",
        status: "",
      }),
        (this.pager = {
          page: 1,
          limit: 10,
        });
      this.getList();
    },
    async failFileClick(id, file_name) {
      const res = await this.axios({
        method: "GET",
        url:
          this.apis.importFileRecordFailFileExport +
          "?import_file_record_id=" +
          id,
        responseType: "blob",
      });
      console.log(res, "res");
      downLoadTempFunc(res, file_name);
    },
    pageChange(e) {
      console.log(e, "e");
      this.pager.page = e;
      this.getList();
    },

    async getSelectList() {
      const res1 = await this.axios.post(this.apis.dataSourceQuery, {
        page: 1,
        limit: 10,
      });
      if (res1.retCode === 2000) {
        this.dataOrigins = res1.rows.items;
      }

      const res2 = await this.axios.post(this.apis.dataTypeQuery, {
        page: 1,
        limit: 10,
      });
      if (res2.retCode === 2000) {
        this.addressObjects = res2.rows.items;
      }
    },
  },
};
</script>
<style lang="less">
.filelist-main {
  height: 100%;
  .query_list {
    position: relative;
    height: 100%;
    .pagination {
      position: absolute;
      bottom: 10px;
      left: 50%;
      transform: translateX(-50%);
    }
    .el-pager li.active {
      background-color: #409eff;
      border-radius: 3px;
      color: #fff;
    }
    .el-pagination .btn-prev,
    .el-pagination .btn-next {
      padding: 0 4px !important;
    }
  }
}
.filelist-main .el-table__header-wrapper .el-table__cell {
  padding: 5px 0;
  font-size: 12px;
}
.filelist-main .el-table__body-wrapper .el-table__cell {
  padding: 0;
  font-size: 10px;
}

.filelist-main th.el-table__cell {
  background-color: rgba(245, 247, 250, 1);
}
.color1 {
  color: #1da261;
}
.color2 {
  color: #de5346;
}
.color3 {
  color: #f88700;
}
.color4 {
  color: #108ee9;
}
.color5 {
  color: #bc33d7;
}

.filelist-main .el-dialog__body {
  padding-top: 0;
}

.filelist-main .el-upload__tip .tip-item {
  line-height: 20px !important;
  font-size: 10px !important;
}

.filelist-main .el-dialog__header {
  border-bottom: #ececec 1px solid;
}

.dialog_title {
  display: flex;
  justify-content: flex-start;
  align-items: flex-end;
}
.filelist-main .el-form-item__label {
  font-size: 12px;
  font-weight: 600;
  padding-right: 5px;
}
.dt_text1 {
  font-size: 14px;
  font-weight: 600;
}
.dt_text2 {
  font-size: 10px;
  color: #444444;
  margin-left: 10px;
}
</style>
